public class Demo {
    public map<String , String> mapFieldToShow{get;set;}
    public map<String , String> mapFieldToShow1{get;set;}
    list<String> lstfield=new list<String>();
    list<String> lstflabel=new list<String>();
    
    public Integer noOfColumns // User input
    { 
       get; 
       set
         {
    
               listFieldToShow = new List<String>(); 
            // listFieldToShow.addAll(lstField); 
               for(integer i=0;i<value;i++)                          //value is user input.
               listFieldToShow.add(lstFlabel[i]);                // we are binding that number of fields.
    
           } 
    }
    
    public List<String> listFieldToShow {get;set;}
    List<String> fieldLabels=new List<String>();
    public  Demo()
    {        
    Contactfields__c objp=[select objectName__c from Contactfields__c limit 1];
    SObjectType objToken = Schema.getGlobalDescribe().get(objp.objectName__c);      
    DescribeSObjectResult objDef = objToken.getDescribe();                    
    Map<String, SObjectField> fieldmap = objDef.fields.getMap(); 
    for(Contactfields__c obj:[select fieldName__c from Contactfields__c order by Fieldvalue__c]) // query from custom settings.
    lstfield.add(obj.fieldName__c);
    listFieldToShow = new List<String>();
    mapFieldToShow = new Map<String , String>();
    mapFieldToShow1 = new Map<String , String>();
    for(integer i=0;i<lstfield.size();i++){
    SObjectField fieldToken = fieldmap.get(lstfield[i]);
    DescribeFieldResult selectedField = fieldToken.getDescribe(); 
    lstflabel.add(selectedfield.getLabel());
    mapFieldToShow.put(selectedfield.getLabel() ,selectedfield.getLocalName());
    mapFieldToShow1.put(selectedfield.getLabel() ,selectedfield.getLabel());
    }
    
    String squery='select ';                                         // Building Dynamic Query.
    for(integer i=0;i<lstfield.size()-1;i++) 
    squery=squery+''+lstfield[i]+',';
    squery=squery+''+lstfield[lstfield.size()-1];
    squery=squery +' '+'from Contact limit 20';
    System.debug('Squeryyyyyyyy'+squery);
    listTableResult =Database.query(squery); 
    
    
    }
    public List<Contact> listTableResult {
    get;set;
    }
    
    public PageReference Add() 
    {
    
                return ApexPages.currentpage();
    
    }
    public List<Contact> getlistTableResult() {
    
    return listTableResult;
    }
    public void setlistTableResult(List<Contact> Contacts) {
    listTableResult = Contacts;
    
    }
    public PageReference save() {   // to save updates.
    upsert listTableResult;
    return null;
    }
    }